package com.service3;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author: LCG
 * 作者: LCG
 * 日期: 2020/11/9  15:22
 * 描述:
 */
public class TestServerMain {

    public static void main(String[] args) throws IOException, ClassNotFoundException, IllegalAccessException, InstantiationException, NoSuchMethodException, InvocationTargetException {

        ServerSocket serverSocket=new ServerSocket();
        serverSocket.bind(new InetSocketAddress("localhost",10000));


        while (true){

            System.out.println("========启动=======");
            Socket accept = serverSocket.accept();

            InputStream inputStream = accept.getInputStream();

            ObjectInputStream ois=new ObjectInputStream(accept.getInputStream());

            //客户端传递的数据
            //传递的参数
            Object[] pargs=(Object[]) ois.readObject();
            //实现类的名称
            String className=(String)ois.readUTF();
            //方法的名称
            String methodName=(String)ois.readUTF();
            //参数类型
            Class<?> [] paramterTypes=(Class<?> [])ois.readObject();


            //处理请求
            Class<?> aClass = Class.forName(className);

            Object o = aClass.newInstance();

            //获取对应的Method
            Method method = aClass.getMethod(methodName, paramterTypes);
            
            //运行Method
            Object result = method.invoke(o, pargs);

            //将结果写回客户端
            OutputStream outputStream = accept.getOutputStream();

            ObjectOutputStream objectOutputStream=new ObjectOutputStream(outputStream);

            //返回给客户端结果
            objectOutputStream.writeObject(result);

        }


    }

}
